package framework.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.ClipboardManager;
import android.text.Editable;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.widget.EditText;

public class MyEditText extends EditText {

	private Spanned pasteCharSequence;
	private SpannableStringBuilder ssb;
	private static final int ID_PASTE = android.R.id.paste;
	private static final int ID_COPY = android.R.id.copy;
	private static int MAX_COUNT = 10;

	public MyEditText(Context context) {
		super(context);
	}

	public MyEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public MyEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public void setMaxCount(int maxCount) {
		this.MAX_COUNT = maxCount;
	}

	@Override
	public boolean onTextContextMenuItem(int id) {
		int selStart = getSelectionStart();
		int selEnd = getSelectionEnd();
		Editable mText = getText();
		ClipboardManager clip = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);

		if (!isFocused()) {
			selStart = 0;
			selEnd = getText().length();
		}

		int min = Math.min(selStart, selEnd);
		int max = Math.max(selStart, selEnd);

		if (min < 0) {
			min = 0;
		}
		if (max < 0) {
			max = 0;
		}

		switch (id) {
		case ID_COPY:
			CharSequence subSequence = mText.subSequence(min, max);
			ssb = new SpannableStringBuilder(subSequence);
			ImageSpan[] spans = ssb.getSpans(0, subSequence.length(), ImageSpan.class);
			for (int i = 0; i < spans.length; i++) {
				ImageSpan span = spans[i];
				String c = span.getSource();
				int a = ssb.getSpanStart(span);
				int b = ssb.getSpanEnd(span);
				ssb.replace(a, b, "<img src='" + c + "'/>");
			}
			clip.setText(ssb);
			return true;
		case ID_PASTE:
			CharSequence text = clip.getText();
			Spanned fromHtml = null;
			if (ssb != null && text.toString().trim().equals(ssb.toString().trim())) {
				fromHtml = Html.fromHtml(ssb.toString(), imageGetter, null);
			} else {
				fromHtml = Html.fromHtml(text.toString(), imageGetter, null);
			}

			float textCount = getTextCount(fromHtml.toString());
			if (getTextCount(mText.toString()) + textCount <= MAX_COUNT) {
				CharSequence subSequence2 = mText.subSequence(0, min);
				CharSequence subSequence3 = mText.subSequence(max, mText.length());
				setText(subSequence2);
				append(fromHtml);
				append(subSequence3);
				setSelection((int) (min + fromHtml.toString().length()));
			}

			return true;
		}

		return super.onTextContextMenuItem(id);
	}

	ImageGetter imageGetter = new ImageGetter() {
		public Drawable getDrawable(String source) {
			int id = Integer.parseInt(source);
			Drawable d = getResources().getDrawable(id);
			d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
			return d;
		}
	};

	private float getTextCount(String text) {
		float chineseCount = (text.getBytes().length - text.length()) / 2;
		return chineseCount + (text.length() - chineseCount) / 2;
	}

}
